<?php
class MatchAction extends CommonAction{
	//选择参赛题目
	public function showMatch(){
		$team = M('Team')->where('Id='.$this->_get('id'))->find();
		//过滤非法操作URL
		$match = new MatchModel($this->_get('id'));
		if((!$match->teamid) || ($team['Captain'] != $_SESSION[C('USER_AUTH_KEY')]['UserId'])){
			echo '<script>alert("非法操作");window.history.back();</script>';
		}
		//判断是否已选择商业问题
		$res = M('Project')->where('TeamId='.$this->_get('id'))->find();
		if($res){
			$ename = M('Enterprise')->where('Id='.$res['EnterpriseId'])->find();//企业名
			$pname = M('Businessproblems')->where('Id='.$res['ProblemId'])->find();//问题名称
			$this->assign('ename',$ename);
			$this->assign('pname',$pname);
		}
		$this->assign('res',$res);
		$this->display();
	}

	
	//提交参赛方案display

	
	public function modProject(){
		$team = M('Team')->where('Id='.$this->_get('id'))->find();
		//过滤非法操作URL
		$match = new MatchModel($this->_get('id'));
		if((!$match->teamid) || ($team['Captain'] != $_SESSION[C('USER_AUTH_KEY')]['UserId'])){
			echo '<script>alert("非法操作");window.history.back();</script>';
		}
		$res = M('Project')->where('TeamId='.$this->_get('id'))->find();
		$this->assign('res',$res);
		$this->display();
	} 


	//处理参赛方案
	public function doModProject(){
		$project = M('project');
		$data['Name'] = $this->_post('Title');                  	//方案名称
		$data['Summarize'] = $this->_post('Summary');  				//方案概述			
		$data['ChoiceEx'] = $this->_post('ChoiceExs');  			//团队分工
		$data['Divide']	= $this->_post('Divide');					//选题经过
		$data['IsPublic'] = $this->_post('VisitSet');
		
		$res = $project->where('TeamId='.$this->_post('id'))->save($data); 
		//var_dump($data);	
		if($res){
			$this->redirect('Student/Match/modTheme',array('id'=>$this->_post('id')));

		}else{
			$this->error('更改失败');
		}
	}


	//详细方案的显示

	
	public function uploadProjectFile(){
		$team = M('Team')->where('Id='.$this->_get('id'))->find();
		//过滤非法操作URL
		$match = new MatchModel($this->_get('id'));
			if((!$match->teamid) || ($team['Captain'] != $_SESSION[C('USER_AUTH_KEY')]['UserId'])){
				echo '<script>alert("非法操作");window.history.back();</script>';
			}
		$uploadfile = M('projectattachfile');
		$project = M('project')->where('TeamId='.$this->_get('id'))->find();
		$resfiles = $uploadfile->where('projectid = '.$project['Id'])->limit(3)->select();
		$this->assign('resfiles',$resfiles);// 赋值数据集
		$this->display(); // 输出模板
		
	}


	//处理详细方案
	public function douploadProjectFile(){
		$file = M('projectattachfile');
			$resfile = M('project')->where('teamId='.$this->_post('id'))->find();
			$data['ProjectId'] = $resfile['Id'];
			$data['FileName'] =$_SESSION['filename'];
			$data['OldName'] =$_SESSION['oldname'];
			$data['categories'] = 1;
			$list = $file->add($data);
			if($list){
				$this->redirect('Student/Match/addWorks',array('id'=>$this->_post('id')));
				echo "<script>window.parent.uploadCallBack('{$info[0]['savename']}','{$info[0]['name']}');</script>";
			}else{
				$this->error('保存失败');
			}

	}


	//选择参赛主题

	
	public function modTheme(){
		$team = M('Team')->where('Id='.$this->_get('id'))->find();
		//过滤非法操作URL
		$match = new MatchModel($this->_get('id'));
			if((!$match->teamid) || ($team['Captain'] != $_SESSION[C('USER_AUTH_KEY')]['UserId'])){
				echo '<script>alert("非法操作");window.history.back();</script>';
			}

		$res = M('Theme');
		$list = $res->limit(6)->select();
		$this->assign('list',$list);
		
		$tid = M()->query('select t.Id
						   from project p,projecttheme pt,theme t
						   where p.Id=pt.ProjectId and pt.ThemeId=t.Id and p.TeamId='.$this->_get('id'));
		$this->assign('tid',$tid);

		$this->display();
	}


	//处理参赛主题 提交
	public function doModTheme(){
		$theme = M('Projecttheme');
		$res = M('Project')->where('TeamId='.$this->_post('id'))->find();
		$data['ThemeId'] = $this->_post('Themes');
		$data['ProjectId'] = $res['Id'];
		$result = $theme->where('ProjectId='.$res['Id'])->find();
		$name = M('Theme')->where('Id='.$this->_post('Themes'))->find(); 
		$jury = M('Juryproject')->where('TeamId='.$this->_post('id').' and ProjectId='.$res['Id'])->setField('ThemeName',$name['Name']);
			//Juryproject 表关联 TeamId projectId  
		if($result && $jury){
			if($theme->where('ProjectId='.$res['Id'])->save($data)){

				$this->redirect('Student/Match/uploadProjectFile',array('id'=>$this->_post('id')));
			}else{
				$this->error('更新失败');

			}
		}else{
			if($theme->add($data)){

				$this->redirect('Student/Match/uploadProjectFile',array('id'=>$this->_post('id')));
			}else{
				$this->error('新增失败');

			}
		}
	}

	
	//实践作品展示

		
	public function addWorks(){
		$team = M('Team')->where('Id='.$this->_get('id'))->find();
		//过滤非法操作URL
		$match = new MatchModel($this->_get('id'));
		if((!$match->teamid) || ($team['Captain'] != $_SESSION[C('USER_AUTH_KEY')]['UserId'])){
			echo '<script>alert("非法操作");window.history.back();</script>';
		}
		$addworks = M('Works');
		$works = $addworks->limit(3)->where('CreateUser='.$_SESSION[C('USER_AUTH_KEY')]['UserId'])->select();
		$this->assign('works',$works);
		$this->display();
	}


	//处理实践作品展示
	public function doaddWorks(){
		$add = M('Works');
		$resworks = M('Project')->where('TeamId='.$this->_post('id') )->find();
		$data['Name'] = $this->_post('Titles');
		$data['ProjectId'] = $resworks['Id'];
		$data['Platform'] = $this->_post('Platforms');
		$data['Url'] = $this->_post('URL');
		$data['Description'] = $this->_post('Data');
		$data['CreateDate'] = date("Y-m-d H:i:s");
		$data['CreateUser'] = $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		$list = $add->add($data);
		if($list){
			$this->redirect('Student/Match/addWorks',array('id'=>$this->_post('id')));
		}else{
			$this->error('保存失败');
		}

	//

	}

	//添加附件
	public function proupfile(){
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		//$upload->saveRule =  NULL;
		$upload->maxSize  = 2*1024*1024 ;// 设置附件上传大小
		$upload->allowExts  = array('doc', 'docx','rar', 'pdf','txt','jpg','png','gif','bmp');// 设置附件上传类型
		$upload->savePath =  './UpFile/Doc/UploadFile/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			echo "<script>window.parent.uploadCallBack(false);</script>";
			$this->error($upload->getErrorMsg());
			
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$file = M('projectattachfile');
			$resfile = M('project')->where('teamId='.$this->_post('id'))->find();
			$data['projectid'] = $resfile['Id'];
			$_SESSION['filename'] = $info[0]['savename'];
			$_SESSION['oldname'] = $info[0]['name'];
			$data['categories'] = 1;
			//$list = $file->add($data);
			
				//$this->redirect('Student/Match/addWorks',array('id'=>$this->_post('id')));
				echo "<script>window.parent.uploadCallBack('{$info[0]['savename']}','{$info[0]['name']}');</script>";
			
	
		}
	}
	//返回企业
	public function returnEnterprises(){
		$data = M('Enterprise')->select();
		if($data){
			$this->ajaxReturn($data,'OK',1);
		}else{
			$this->ajaxReturn($data,'数据库加载失败！',-1);
		}
		
	}
	//返回商业问题
	public function returnProlems(){
		$eid = $this->_post('eId');
		$data = M('Businessproblems')->where('EnterpriseId='.$eid)->select();
		if($data){
			$this->ajaxReturn($data,'OK',1);
		}else{
			$this->ajaxReturn($data,'数据库加载失败！',-1);
		}
	}
	//选择给定的1
	public function SelectExist(){
		//2次验证
		$validate = array(
			array('ProjectType','require','项目必须！'), // 验证
			array('ProblemId','require','问题必须！'), // 验证
			array('EnterpriseId','require','企业必须！'), // 验证
		);
		$data['ProjectType'] = '企业项目';
		$data['ProblemId'] = $this->_post('problemId');
		$data['EnterpriseId'] = $this->_post('enterpriseId');
		$data['TeamId'] = $this->_post('teamId');
		$data['CreateTime'] = date('Y-m-d H:i:s');
		$data['LastUpdate'] = date('Y-m-d H:i:s');
		$projectid = M('Project')->validate($validate)->add($data);
		//方案评判表
		$jury['ProjectId'] = $projectid;
		$jury['TeamId'] = $this->_post('teamId');
		//判断阶段
		$team = M('Team')->where('Id='.$this->_post('teamId'))->find();
		$jury['Section'] = $team['Status'];
		$jid = M('Juryproject')->add($jury);
		if($projectid > 0 && $jid > 0){
			$this->ajaxReturn($data,'OK',1);
		}else{
			$this->ajaxReturn($data,'选择商业问题失败！',-1);
		}
	}
	//自拟问题2
	public function SelectSelfProblem(){
		//自拟竞赛问题
		$busi['Title'] = $this->_post('problemTitle');
		$busi['EnterpriseId'] = $this->_post('enterpriseId');
		$busi['Goal'] = $this->_post('goal');
		$busi['Description'] = $this->_post('description');
		$busi['CreateDate'] = date('Y-m-d H:i:s');
		$busi['CreateUser'] = $_SESSION[C('USER_AUTH_KEY')]['UserId'];//用户名
		$busi['IsPublic'] = 0;
		$busi['IsApproved'] = 1;
		$busi['UpdateTiem'] = date('Y-m-d H:i:s');
		$busi['ChoiceCount'] = 1;
		$busi['ViewCount'] = 1;
		$validate = array(
			array('Title','require','商业问题名称必须！'), // 验证
			array('Goal','require','商业问题目标必须！'), // 验证
			array('Description','require','商业问题描述必须！'), // 验证
			array('EnterpriseId','require','企业必须！'), // 验证
		);
		$businessid = M('Businessproblems')->validate($validate)->add($busi);
		//proejct
		$data['EnterpriseId'] = $this->_post('enterpriseId');
		$data['TeamId'] = $this->_post('teamId');
		$data['ProjectType'] = '企业项目-自定义问题';
		$data['ProblemId'] = $businessid;
		$data['CreateTime'] = date('Y-m-d H:i:s');
		$data['LastUpdate'] = date('Y-m-d H:i:s');
		$validate = array(
			array('ProjectType','require','项目必须！'), // 验证
			array('EnterpriseId','require','企业必须！'), // 验证
		);
		$projectid = M('Project')->validate($validate)->add($data);
		//方案评判表
		$jury['ProjectId'] = $projectid;
		$jury['TeamId'] = $this->_post('teamId');
		//判断阶段
		$team = M('Team')->where('Id='.$this->_post('teamId'))->find();
		$jury['Section'] = $team['Status'];
		$jid = M('Juryproject')->add($jury);
		if($projectid > 0 && $businessid > 0 && $jid > 0){
			$this->ajaxReturn('成功','OK',1);
		}else{
			$this->ajaxReturn('错误','选择商业问题失败！',-1);
		}
	}
	//拓展企业3
	public function SelectExpandEnterprise(){
		//拓展企业
		$enter['Name'] = $this->_post('enterpriseName');
		$enter['LinkMan'] = $this->_post('contactor');
		$enter['LinkManJob'] = $this->_post('job');
		$enter['TelePhone'] = $this->_post('phoneNumber');
		$validate = array(
			array('Name','require','企业名称必须！'), // 验证
			array('LinkMan','require','联系人标必须！'), // 验证
			array('LinkManJob','require','联系人职务必须！'), // 验证
			array('TelePhone','require','联系人电话必须！'), // 验证
		);
		$enterpriseid = M('Enterprise')->validate($validate)->add($enter);
		//自拟竞赛问题
		$busi['Title'] = $this->_post('problemTitle');
		$busi['EnterpriseId'] = $enterpriseid;
		$busi['Goal'] = $this->_post('goal');
		$busi['Description'] = $this->_post('description');
		$busi['CreateDate'] = date('Y-m-d H:i:s');
		$busi['CreateUser']=$_SESSION[C('USER_AUTH_KEY')]['UserId'];//用户名
		$busi['IsPublic'] = 0;
		$busi['IsApproved'] = 1;
		$busi['UpdateTiem'] = date('Y-m-d H:i:s');
		$busi['ChoiceCount'] = 1;
		$busi['ViewCount'] = 1;
		$validate = array(
			array('Title','require','商业问题名称必须！'), // 验证
			array('Goal','require','商业问题目标必须！'), // 验证
			array('Description','require','商业问题描述必须！'), // 验证
		);
		$businessid = M('Businessproblems')->validate($validate)->add($busi);
		//proejct
		$data['EnterpriseId'] = $enterpriseid;
		$data['TeamId'] = $this->_post('teamId');
		$data['ProjectType'] = '自行扩展企业-自定义问题';
		$data['ProblemId'] = $businessid;
		$data['CreateTime'] = date('Y-m-d H:i:s');
		$data['LastUpdate'] = date('Y-m-d H:i:s');
		$projectid=M('Project')->add($data);
		//方案评判表
		$jury['ProjectId'] = $projectid;
		$jury['TeamId'] = $this->_post('teamId');
		//判断阶段
		$team = M('Team')->where('Id='.$this->_post('teamId'))->find();
		$jury['Section'] = $team['Status'];
		$jid = M('Juryproject')->add($jury);
		if($enterpriseid > 0 && $projectid > 0 && $businessid > 0 && $jid > 0){
			$this->ajaxReturn('成功','OK',1);
		}else{
			$this->ajaxReturn('错误','选择商业问题失败！',-1);
		}
	}
	//创业项目4
	public function SelectCreate(){
		//自拟竞赛问题
		$busi['Title'] = $this->_post('problemTitle');
		//$busi['EnterpriseId'] = $this->_post('enterpriseId');
		$busi['Goal'] = $this->_post('goal');
		$busi['Description'] = $this->_post('description');
		$busi['CreateDate'] = date('Y-m-d H:i:s');
		$busi['CreateUser'] = $_SESSION[C('USER_AUTH_KEY')]['UserId'];//用户名
		$busi['IsPublic'] = 0;
		$busi['IsApproved'] = 1;
		$busi['UpdateTiem'] = date('Y-m-d H:i:s');
		$busi['ChoiceCount'] = 1;
		$busi['ViewCount'] = 1;
		$validate = array(
			array('Title','require','商业问题名称必须！'), // 验证
			array('Goal','require','商业问题目标必须！'), // 验证
			array('Description','require','商业问题描述必须！'), // 验证
		);
		$businessid = M('Businessproblems')->validate($validate)->add($busi);
		//proejct
		//$data['EnterpriseId'] = $this->_post('enterpriseId');
		$data['TeamId'] = $this->_post('teamId');
		$data['ProjectType'] = '创业项目';
		$data['ProblemId'] = $businessid;
		$data['CreateTime'] = date('Y-m-d H:i:s');
		$data['LastUpdate'] = date('Y-m-d H:i:s');
		$projectid = M('Project')->add($data);
		//方案评判表
		$jury['ProjectId'] = $projectid;
		$jury['TeamId'] = $this->_post('teamId');
		//判断阶段
		$team = M('Team')->where('Id='.$this->_post('teamId'))->find();
		$jury['Section'] = $team['Status'];
		$jid = M('Juryproject')->add($jury);
		if($projectid > 0 && $businessid > 0 && $jid > 0){
			$this->ajaxReturn('成功','OK',1);
		}else{
			$this->ajaxReturn('错误','选择商业问题失败！',-1);
		}
	}
	//确认选择参赛题目
	public function sureMatch(){
		//echo '<pre>';debug块
		echo $_SESSION[C('USER_AUTH_KEY')]['UserId'];
		//$team=M('Team')->where('Id='.$_SESSION[C('USER_AUTH_KEY')]['UserId'])->find();
		$user=M('Users')->where("UserId=".$_SESSION[C('USER_AUTH_KEY')]['UserId'])->find();
		var_dump($user);
	}
	
	//删除提交的方案的附件
	public function delfile(){
		$delfile=$this->_post('filename');
		//删除文件
		@unlink('./UpFile/Doc/UploadFile/'.$delfile);
		//删除数据库
		$file = M("Projectattachfile"); // 实例化User对象
		$res = $file->where('filename ="'.$delfile.'"')->delete(); 
		if($res){
			$this->ajaxReturn('','删除成功',1);
		}else{
			$this->ajaxReturn('','删除失败',-1);
		}
		
		
	}
	//删除实践作品
	public function delworks(){
		$Id=$this->_post('fileid');
		//删除数据库
		$shijianwork = M("Works"); // 实例化Works对象
		$shijianwork->where('Id ="'.$Id.'"')->delete(); 
		
	}
	//编辑作品信息
	public function EditaddWorks(){
 		$edit = M("Works");
		$list = $edit->where('Id='.$this->_get('id'))->select();
		$this->assign('list',$list);
		$this->display();
	}
	public function doEditaddWorks(){
		$doedit = M("Works");
		$data['Name'] = $this->_post('names');
		$data['Platform'] = $this->_post('platform');
		$data['Url'] = $this->_post('urls');
		$data['OtherInfo'] = $this->_post('otherinfos');
		$list = $doedit->where('Id='.$this->_post('id'))->save($data);
		if($list){
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
	
	}
}